using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;

namespace M0
{
    public class 摇杆 : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
        [System.Serializable]
        public class Event : UnityEvent<Vector2>
        {
        }

        [Header("Rect References")] public RectTransform containerRect;
        public RectTransform handleRect;

        [Header("Settings")] public float joystickRange = 50f;
        public float magnitudeMultiplier = 1f;
        public bool invertXOutputValue;
        public bool invertYOutputValue;

        [Header("Output")] public Event joystickOutputEvent;

        public CanvasGroup 显示面板;
        public Vector2 显示面板程度;

        public void Show(bool b)
        {
            显示面板.alpha = b ? 显示面板程度.x : 显示面板程度.y;
        }

        // public bool 隐藏模式;

        void Start()
        {
            SetupHandle();
        }

        private void SetupHandle()
        {
            if (handleRect)
            {
                UpdateHandleRectPosition(Vector2.zero);
            }
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if(M.点到Ui(1)) return;
            // OnDrag(eventData);
            b开始拖动 = true;
            // 摇杆.gameObject.SetActive(true);
            // var pos = Input.mousePosition;
            
            //ToDo 跟着移动
            // var pos = eventData.position;
            // transform.position = pos;
            Show(true);
        }

        private Camera _camera;
        private bool b开始拖动;

        public void Update()
        {
            if (b开始拖动)
            {
                Vector3 pos = Input.mousePosition;

                RectTransformUtility.ScreenPointToLocalPointInRectangle(containerRect, pos, _camera,
                    out Vector2 position);

                position = ApplySizeDelta(position);

                Vector2 clampedPosition = ClampValuesToMagnitude(position);

                Vector2 outputPosition = ApplyInversionFilter(position);

                OutputPointerEventValue(outputPosition * magnitudeMultiplier);

                if (handleRect)
                {
                    UpdateHandleRectPosition(clampedPosition * joystickRange);
                }
            }
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            OutputPointerEventValue(Vector2.zero);

            if (handleRect)
            {
                UpdateHandleRectPosition(Vector2.zero);
            }

            b开始拖动 = false;
            Show(false);
        }

        private void OutputPointerEventValue(Vector2 pointerPosition)
        {
            joystickOutputEvent.Invoke(pointerPosition);
        }

        private void UpdateHandleRectPosition(Vector2 newPosition)
        {
            handleRect.anchoredPosition = newPosition;
        }

        Vector2 ApplySizeDelta(Vector2 position)
        {
            float x = (position.x / containerRect.sizeDelta.x) * 2.5f;
            float y = (position.y / containerRect.sizeDelta.y) * 2.5f;
            return new Vector2(x, y);
        }

        Vector2 ClampValuesToMagnitude(Vector2 position)
        {
            return Vector2.ClampMagnitude(position, 1);
        }

        Vector2 ApplyInversionFilter(Vector2 position)
        {
            if (invertXOutputValue)
            {
                position.x = InvertValue(position.x);
            }

            if (invertYOutputValue)
            {
                position.y = InvertValue(position.y);
            }

            return position;
        }

        float InvertValue(float value)
        {
            return -value;
        }

    }
}
